/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.KeyNamePairs;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.BusServiceRel;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.List;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @Author Kent
 */
public class ServiceBusBindingContent extends Content {
	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;

	private VerticalPanel panel = null;

	private ListBox bus;

	private BusServiceRel rel;

	private AsyncCallback<ResponseObject> cb1 = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			Window.alert(cons.modifyOK());

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public ServiceBusBindingContent(ServiceMessages cons,
			BifangGWTServiceAsync s, GwtContext c, BusServiceRel rel) {
		this.service = s;
		this.cons = cons;
		this.context = c;
		this.rel = rel;

		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");

		initWidget(panel);

	}

	public void onLoad() {
		HTML h = new HTML(cons.bindingBus2());
		panel.add(h);
		bus = new ListBox();
		bus.setWidth("11em");

		List ret = (List) context.get(ServiceConstants.BUS_LIST);
		// bus.setVisibleItemCount(10);
		bus.addItem("", "");

		for (int i = 0, j = ret.size(); i < j; i++) {
			KeyNamePairs k = (KeyNamePairs) ret.get(i);
			bus.addItem(k.getName(), k.getId());

			if (k.getId().equals(rel.getBusId())) {
				bus.setSelectedIndex(i + 1);
			}
		}

		panel.add(bus);
		panel.add(new HTML("&nbsp;"));

		Button b1 = new Button(cons.modify());
		b1.addClickListener(new ClickListener() {
			public void onClick(Widget send) {
				if (bus.getSelectedIndex() > 0) {
					rel.setBusId(bus.getValue(bus.getSelectedIndex()));
				} else {
					rel.setBusId("");
				}

				RequestObject req = new RequestObject();
				req
						.setAction("han.bifang.gwt.server.action.service.ModifyBusServiceRelAction");
				req.setRequest(rel);
				service.call(req, cb1);
			}

		});

		if (PrivilegChecker.hasPrivilege(context, null))
			panel.add(b1);

	}

}
